package gradle.java.demo;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Flow;

@Slf4j
public class MsgSubscriber implements Flow.Subscriber<String> {

    private Flow.Subscription subscription;

    @Override
    public void onSubscribe(Flow.Subscription subscription) {
        this.subscription = subscription;
        this.subscription.request(1);
    }

    @Override
    public void onNext(String item) {
        log.info("SubscriberReceiveMsg={}", item);
        this.subscription.request(1);
    }

    @Override
    public void onError(Throwable throwable) {
        log.error("error", throwable);
        this.subscription.cancel();
    }

    @Override
    public void onComplete() {
        log.info("SubscriberComplete!");
    }
}
